package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

/**
 * @author Xiao
 * @date 2021/7/17
 */
public class StudentDao_2 implements StudentDao_Base{
    private static ArrayList<Student> list = new ArrayList<>();
    //静态代码块
    static{
        Student stu1 = new Student("itheima001","张三",23,"19980808");
        Student stu2 = new Student("itheima002","李四",24,"19970707");
        list.add(stu1);
        list.add(stu2);
    }
    //增：
    @Override
    public boolean addStudent(Student stu) {
        return list.add(stu);
    }

    //查
    @Override
    public Student[] findAllStudent() {
        Student[] stuArr = new Student[list.size()];
        for (int i = 0; i < list.size(); i++) {
            stuArr[i] = list.get(i);
        }
        return stuArr;
    }

    //删
    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        list.remove(index);
    }

    //改
    @Override
    public void updateStudent(String sid, Student stu) {
        int index = getIndex(sid);
        list.set(index, stu);
    }

    //根据学号查找对象索引
    @Override
    public int getIndex (String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null && sid.equals(stu.getId())) {
                index = i;
                break;
            }
        }
        return index;
    }
}
